import {message} from 'antd';
import { getChargeType, addChargeType, updateChargeType } from '../../services/store';

export default {
    namespace: 'charge',
    state: {
        /*
         * 收费数据包含消费类型基础数据
         * */
        baseData: {
            charge_type: [
                {
                    id: 'ct_0',
                    name: '计次消费',
                },
                {
                    id: 'ct_1',
                    name: '计时消费',
                },
                {
                    id: 'ct_2',
                    name: '实物消费',
                },
            ]
        },
        data: [],
        editorData: {},
        visible: false,
    },
    subscriptions: {
        setup(args) {
            const { dispatch, history } = args;
            history.listen(({ pathname }) => {
                if (pathname === '/home/charge') {
                    message.warning('功能尚未开放，仅模拟交互', 5);
                    dispatch({
                        type: 'init',
                    });
                }
            });
        }
    },
    effects: {
        *init(payload, { put }){
            yield put({
                type: 'load',
            });
        },
        *load(payload, { put, call, select }) {
            const { storeId } = yield select((state => state.app.user));
            const { data, err, code } = yield call(getChargeType, storeId);
            if (code === '0000') {
                yield put({
                    type: 'setState',
                    payload: {
                        data,
                    },
                });
            } else {
                console.log(err);
            }
        },
        *editorOk({ payload: {err, isNew, values}}, {call, put, select}){
            if (err) {
                return;
            }
            let res;
            if (!isNew) {
                res = yield call(updateChargeType, values);
            } else {
                const {storeId} = yield select(state => state.app.user);
                const body = {...values, storeId};
                res = yield call(addChargeType, body);
            }
            const {code, msg, err: e} = res;
            if (code === '0000') {
                message.success(msg);
                yield [
                    put({
                        type: 'editorVisible',
                    }),
                    put({
                        type: 'load',
                    }),
                ];
            } else {
                message.error(msg || e);
            }
        }
    },
    reducers: {
        setState(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        editorVisible(state, { payload: { editorData } = {} }){
            // 隐藏时不清数据,避免因为隐藏动作慢而界面显示为新建
            if (editorData) {
                return {
                    ...state,
                    visible: !!editorData,
                    editorData,
                };
            } else {
                return {
                    ...state,
                    visible: false,
                    editorData: {}
                };
            }
        },
    }
};
